﻿using System;
using System.Web.UI.WebControls;
using Bingosoft.Modules.DataDictionary;
using Bingosoft.Web.Utils;

namespace Bingosoft.WebApp.Modules.DataDictionary
{
    public partial class DataDictionaryItemList : System.Web.UI.ListPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SearchGrid(object sender, EventArgs e)
        {
            this.GridSearch.Search();
        }

        protected void RefreshGrid(object sender, EventArgs e)
        {
            this.GridSearch.Refresh();
        }

        protected void DeleteDictionaryItem(object sender, EventArgs e)
        {
            var _sender = sender as ImageButton;
            String dictionaryItemId = _sender.Attributes["_KeyId"];

            this.DataDictionaryService.DeleteDataDictionaryItem(dictionaryItemId);

            RefreshGrid(sender, e);

            ScriptHelper.RegisterScript(this, "window.parent.reloadTree();");
        }

        protected void Disable(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            string id = img.Attributes["_KeyId"];
            this.DataDictionaryService.Disable(id);

            RefreshGrid(sender, e);
        }

        protected void Enable(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            string id = img.Attributes["_KeyId"];
            this.DataDictionaryService.Enable(id);

            RefreshGrid(sender, e);
        }

        protected void SmartGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton enableImgBtn = e.Row.FindControl("EnableImageButton") as ImageButton;
                ImageButton disableImgBtn = e.Row.FindControl("DisableImageButton") as ImageButton;
                string status = enableImgBtn.Attributes["_STATUS"];
                if ("enabled".Equals(status))
                {
                    enableImgBtn.Visible = false;
                    disableImgBtn.Visible = true;
                }
                else
                {
                    enableImgBtn.Visible = true;
                    disableImgBtn.Visible = false;
                }
            }
        }

        private DataDictionaryService _dataDictionaryService;
        private DataDictionaryService DataDictionaryService
        {
            get
            {
                return _dataDictionaryService ??
                       (_dataDictionaryService = ObjectHelper.GetObject<DataDictionaryService>());
            }
        }
    }
}